use dataset_polls, clear

format date %dm_Y

local c2000a = date("11-07-2000", "MDY")
local c2000b = date("12-12-2000", "MDY")
local c2000c = date("11-02-2004", "MDY")
local c2016 = date("11-08-2016", "MDY")
local c2020 = date("11-03-2020", "MDY")

forvalues i = 1(1)3 {

preserve

	if `i' == 1 {
		gen x = date - `c2000b'
		local c2000c = `c2000c' - `c2000b'
		keep if year(date) < 2005
		local mytitle "(b) 2000, Republicans"
	}	
	if `i' == 2 {
		keep if year(date) > 2005 & year(date) < 2020
		gen x = date - `c2016'
		local mytitle "(d) 2016, Republicans"
	}
	if `i' == 3 {
		keep if year(date) > 2019
		gen x = date - `c2020'
		local mytitle "(f) 2020, Republicans"
	}
	
	#delimit;

	gr tw
		(sc legitimatec x if subsample == "Republican", col(gray)
			msym(O))
		(sc legitimatec x if subsample == "Rep Cndt", col(gray) 
			msym(Oh))
		(lfit legitimatec x if regex(subsample, "Rep") & x < 0, 
			clcol(black) lwid(thick))
		(lfit legitimatec x if regex(subsample, "Rep") & x > 0
			& date!=16396, clcol(black) lwid(thick))
		,
			legend(off)
			plotregion(style(none))
			xtitle("")
			ytitle("")
			ylab(0(0.2)1, angle(horiz))
			`myxlab'
			xline(0, lpat(dash))
			name(g`i', replace)
			title("`mytitle'")
			;
	#delimit cr
	
	gr export "_output/figure_trends_rep_`i'.pdf", replace
	
	gr close
	
restore

}
